﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using Control;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos
{
    public partial class mh_dmnhanvien : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        private byte chitiet = 1;  // nếu chitiet = 0: xem chi tiet; chitiet = 1: click chuyen tab
        public Boolean m_nhanvien_baocao = false;
        xulyngaythangnam ngaythang;
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_dmnhanvien).Assembly);
                this.page_phongban.Text = rm.GetString("phongban", culture);
                this.page_nhanvien.Text = rm.GetString("nhanvien", culture);
                this.grid_phongban.GroupPanelText = rm.GetString("danhsachphongban", culture);
                this.col_tenphongban.Caption = rm.GetString("ten", culture);
                this.col_sodienthoai.Caption = rm.GetString("dienthoai", culture);
                label1.Text = rm.GetString("phongban", culture);
                label2.Text = rm.GetString("dienthoai", culture);

                bton_xemchitietnhanvien.Text = rm.GetString("xemchitietnhanvien", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_clr.Text = rm.GetString("clr", culture);
                bton_tim.Text = rm.GetString("tim", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                //nhan vien
                label9.Text = rm.GetString("tennhanvien", culture);
                label10.Text = rm.GetString("chucvu", culture);
                label8.Text = rm.GetString("phongban", culture);
                label7.Text = rm.GetString("ngaysinh", culture);
                label6.Text = rm.GetString("gioitinh", culture);
                lbel_banle.Text = rm.GetString("dienthoai", culture);
                label3.Text = rm.GetString("diachi", culture);
                label4.Text = rm.GetString("hesoluong", culture);
                label5.Text = rm.GetString("ngaysinh", culture);
                label6.Text = rm.GetString("gioitinh", culture);

                bton_timnhanvien.Text = rm.GetString("tim", culture);
                bton_clrnhanvien.Text = rm.GetString("clr", culture);
                bton_themnhanvien.Text = rm.GetString("them", culture);
                bton_suanhanvien.Text = rm.GetString("sua", culture);
                bton_xoanhanvien.Text = rm.GetString("xoa", culture);
                bton_luunhanvien.Text = rm.GetString("luu", culture);
                bton_chonnhanvien.Text = rm.GetString("chon", culture);
                this.col_phongban_nv.Caption = rm.GetString("phongban", culture);
                this.col_tennhanvien.Caption= rm.GetString("tennhanvien", culture);
                this.col_diachi.Caption= rm.GetString("diachi", culture);
                this.col_gioitinh.Caption= rm.GetString("gioitinh", culture);
                this.col_dienthoai.Caption = rm.GetString("dienthoai", culture);
                bton_thoatnhanvien.Text = rm.GetString("thoat", culture);
                this.col_ngaysinh.Caption = rm.GetString("ngaysinh", culture);
                //label6.Text = rm.GetString("gioitinh", culture);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        // select danh phong ban
        private DataTable f_selectdmphongban()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhanvien", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh nhan vien
        private DataTable f_selectdmnhanvien()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 4 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhanvien", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        private DataTable f_selectdsnhanvientheophongban(string maphongban)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 1, 3, maphongban };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhanvien", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach phong ban
        private DataTable f_selectdsphongbanforcom()
        {
            tb = f_selectdmphongban();
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn phòng ban ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom chu hh co dieu kien
        private DataTable f_selecttenphongbandk(string tenphongban, string sodienthoai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@DIENTHOAI" };
            o = new object[] { 1, 2, tenphongban, sodienthoai };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhanvien", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // insert them phongban
        private int f_insertphongban(string tenphongban, string dienthoai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@DIENTHOAI" };
            o = new object[] { 2, 1, tenphongban, dienthoai };
            int ketqua = tv.SP_Update("sp_mh_dmnhanvien", s, o);
            return ketqua;
        }
       
        // insert them nhan vien
        private int f_insertnhanvien(string tennhanvien, string chuvu, string maphongban, Boolean gioitinh, string ngaysinh,
                                                    string dienthoai, string diachi, decimal luongcoban, decimal hesoluong )
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@CHUCVU","@MAPHONGBAN","@GIOITINH","@NGAYSINH_str",
                                        "@DIENTHOAI","@DIACHI","@LUONGCOBAN","@HESOLUONG"    };
            o = new object[] { 2, 2, tennhanvien, chuvu, maphongban, gioitinh, ngaysinh,
                                                    dienthoai, diachi, luongcoban, hesoluong};
            int ketqua = tv.SP_Update("sp_mh_dmnhanvien", s, o);
            return ketqua;
        }

        // update mot phong ban
        private int f_updatephongban(string maphongban, string tenphongban, string dienthoai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@DIENTHOAI" };
            o = new object[] { 3, 1, maphongban, tenphongban, dienthoai };
            int ketqua = tv.SP_Update("sp_mh_dmnhanvien", s, o);
            return ketqua;
        }
        // update nhan vien
        private int f_updatenhanvien(string manhanvein, string tennhanvien, string chuvu, string maphongban, Boolean gioitinh, string ngaysinh,
                                                   string dienthoai, string diachi, decimal luongcoban, decimal hesoluong)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "OBJID", "@OBJNAME", "@CHUCVU","@MAPHONGBAN","@GIOITINH","@NGAYSINH_str",
                                        "@DIENTHOAI","@DIACHI","@LUONGCOBAN","@HESOLUONG"    };
            o = new object[] { 3, 2,manhanvein, tennhanvien, chuvu, maphongban, gioitinh, ngaysinh,
                                                    dienthoai, diachi, luongcoban, hesoluong};
            int ketqua = tv.SP_Update("sp_mh_dmnhanvien", s, o);
            return ketqua;
        }

        // xoa phong ban
        private int f_deletephongban(string maphongban)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, maphongban };
            int ketqua = tv.SP_Update("sp_mh_dmnhanvien", s, o);
            return ketqua;
        }
        // xoa nhan vien
        private int f_deletenhanvien(string manhanvien)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 2, manhanvien };
            int ketqua = tv.SP_Update("sp_mh_dmnhanvien", s, o);
            return ketqua;
        }
 
        // Tim nhan vien khi nhan nut tim
        private DataTable f_selectfindnhanvien(string tennhanvien, string chuvu, string phongban, Boolean checkngaysinh, string ngaysinh, int gioitinh,
                                                                    string dienthoai, string diachi, decimal luongcoban, decimal hesoluong)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@CHUCVU", "@MAPHONGBAN", "@CHECKNGAYSINH", "@NGAYSINH_str","@INT_GIOITINH", 
                                            "@DIENTHOAI", "@DIACHI", "@LUONGCOBAN","@HESOLUONG"};
            o = new object[] { 1, 5, tennhanvien, chuvu, phongban, checkngaysinh, ngaysinh, gioitinh,
                                        dienthoai, diachi, luongcoban, hesoluong };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhanvien", s, o);
            return tb;
        }

        // tim hang hoa
        private DataTable f_findnhanvien()
        {
            DataTable tb;
            string tennhanvien;
            string chucvu;
            string phongban;
            int gioitinh = 1;
            string dienthoai;
            string diachi;
            decimal luongcoban;
            decimal hesoluong;
            Boolean checkngaysinh;
            string ngaysinh="";

            if (txt_tennhanvien.Text != "")
                tennhanvien = "'%" + txt_tennhanvien.Text + "%'";
            else
                tennhanvien = "";

            if (txt_chucvu.Text != "")
                chucvu = "'%" + txt_chucvu.Text + "%'";
            else
                chucvu = "";

            if (com_phongban.Text != "--- Chọn phòng ban ---")
                phongban = com_phongban._GetselectedID();
            else
                phongban = "";

            if (check_ngaysinh.Checked == true)
                checkngaysinh = true;
            else
                checkngaysinh = false;
            // handung = Convert.ToDateTime(date_handung.Text);
            ngaythang = new xulyngaythangnam();
            if (date_ngaysinh.Text != "")
                ngaysinh = ngaythang.chuyenngay(date_ngaysinh.Text);
             if (com_gioitinh.Text == "--- Chọn tất cả ---")
                 gioitinh = 1;
             else if (com_gioitinh.Text == "Nam")
                 gioitinh = 2;
             else if (com_gioitinh.Text == "Nữ")
                 gioitinh = 3;

             if (txt_dienthoai.Text != "")
                 dienthoai = "'%" + txt_dienthoai.Text + "%'";
             else
                 dienthoai = "";

             if (txt_diachi.Text != "")
                 diachi = "'%" + txt_diachi.Text + "%'";
             else
                 diachi = "";

             luongcoban = Convert.ToDecimal("0" + txt_luongcoban.Text.Replace(",", ""));
             hesoluong = Convert.ToDecimal("0" + txt_hesoluong.Text);


             tb = f_selectfindnhanvien(tennhanvien, chucvu, phongban, checkngaysinh, ngaysinh, gioitinh,
                                                      dienthoai, diachi, luongcoban, hesoluong);
            return tb;

        }

        public mh_dmnhanvien()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            tv = new thuvien();
            tv.f_showdataoncombox(com_phongban, f_selectdsphongbanforcom(), "OBJID", "OBJNAME");
        }

        private void mh_dmnhanvien_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            luoi_phongban.MainView = grid_phongban;
            grid_phongban.OptionsBehavior.Editable  = false;
            luoi_phongban.DataSource = f_selectdmphongban();
         
            radio_nam.Visible = true;
            radio_nu.Visible = true;
            bton_luu.Enabled = false;
            bton_luunhanvien.Enabled = false;
            bton_tenphongban.Enabled = false;
            bton_sotienthoai.Enabled = false;
         
            bton_tennhanvien.Enabled = false;
            bton_chucvu.Enabled = false;
            bton_dienthoai.Enabled = false;
            bton_diachi.Enabled = false;
            bton_luongcoban.Enabled = false;
            bton_hesoluong.Enabled = false;
            check_ngaysinh.Visible = false;
            com_gioitinh.Visible = false;

            if (m_nhanvien_baocao == true)
            {
                bton_chonnhanvien.Visible = true;
                bton_thoatnhanvien.Visible = false;
                tab_nhanvien.SelectedTabPage = page_nhanvien;
            }
            else
            {
                bton_chonnhanvien.Visible = false;
                bton_thoatnhanvien.Visible = true;
            }

        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tenphongban.Text = "";
            txt_sotienthoai.Text = "";
            bton_tenphongban.Enabled = true;
            bton_sotienthoai.Enabled = true;
            bton_luu.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_themnhanvien_Click(object sender, EventArgs e)
        {
            txt_tennhanvien.Text = "";
            txt_chucvu.Text = "";
            com_phongban.Text = "--- Chọn phòng ban ---";
            //aaa
            //date_ngaysinh.Text = DateTime.Today.ToString("dd/MM/yyyy");
            date_ngaysinh.DateTime = DateTime.Today;
            radio_nam.Checked = true;
            txt_dienthoai.Text = "";
            txt_diachi.Text = "";
            txt_luongcoban.Text = "";
            txt_hesoluong.Text = "";
            bton_luunhanvien.Enabled = true;
            bton_tennhanvien.Enabled = true;
            bton_chucvu.Enabled = true;
            bton_dienthoai.Enabled = true;
            bton_diachi.Enabled = true;
            bton_luongcoban.Enabled = true;
            bton_hesoluong.Enabled = true;
            Variable.str_trangthaithemsua = "them";
            radio_nam.Visible = true;
            radio_nu.Visible = true;
            com_gioitinh.Visible = false;
            check_ngaysinh.Visible = false;
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            //bton_luunhanvien.Enabled = true;
            //bton_tennhanvien.Enabled = true;
            //bton_chucvu.Enabled = true;
            //bton_dienthoai.Enabled = true;
            //bton_diachi.Enabled = true;
            //bton_luongcoban.Enabled = true;
            //bton_hesoluong.Enabled = true;
            bton_luu.Enabled = true;
            bton_tenphongban.Enabled = true;
            bton_sotienthoai.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_suanhanvien_Click(object sender, EventArgs e)
        {
            bton_tennhanvien.Enabled = true;
            bton_chucvu.Enabled = true;
            bton_dienthoai.Enabled = true;
            bton_luongcoban.Enabled = true;
            bton_hesoluong.Enabled = true;
            bton_diachi.Enabled = true;

            //bton_tenphongban.Enabled = true;
            //bton_sotienthoai.Enabled = true;
            bton_luunhanvien.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
            radio_nam.Visible = true;
            radio_nu.Visible = true;
            com_gioitinh.Visible = false;
            check_ngaysinh.Visible = false;
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_phongban.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string maphongban = grid_phongban.GetRowCellValue(grid_phongban.FocusedRowHandle, col_maphongban).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletephongban(maphongban);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_phongban.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_phongban.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_xoanhanvien_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_nhanvien.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string manhanvien = grid_nhanvien.GetRowCellValue(grid_nhanvien.FocusedRowHandle, col_manhanvien).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletenhanvien(manhanvien);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                else
                {
                    MessageBox_LPV.Show("Xóa thành công");
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_nhanvien.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_nhanvien.Views[0].DataSource;
                tam.Delete(chiso);
                radio_nam.Visible =true;
                radio_nu.Visible = true;
                com_gioitinh.Visible = false;
                check_ngaysinh.Visible = false;
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int ketqua;
            int chiso = -1;

            if (txt_tenphongban.Text == "")       // không thể luu neu chua nhap ten phong ban
                return;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertphongban(txt_tenphongban.Text,txt_sotienthoai.Text );

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                chiso = ((ColumnView)luoi_phongban.Views[0]).FocusedRowHandle;
                string maphongban = grid_phongban.GetRowCellValue(grid_phongban.FocusedRowHandle, col_maphongban).ToString();
                ketqua = f_updatephongban(maphongban, txt_tenphongban.Text,txt_sotienthoai.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
            }
            luoi_phongban.DataSource = null;
            luoi_phongban.DataSource = f_selectdmphongban();
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grid_phongban.SelectRow(chiso);
                grid_phongban.FocusedRowHandle = chiso;

            }
        
        }

        private void bton_luunhanvien_Click(object sender, EventArgs e)
        {
            ngaythang = new xulyngaythangnam();
            int ketqua;
            int chiso = -1;

            if (txt_tennhanvien.Text == "" && com_phongban.Text == "--- Chọn phòng ban ---")       // không thể luu neu chua nhap ten nhan vien va phong ban
                return;
           
            string tennhanvien = txt_tennhanvien.Text;
            string chucvu = txt_chucvu.Text;
            string maphongban = com_phongban._GetselectedID();
            string ngaysinh;

            if (date_ngaysinh.Text != "")
                ngaysinh = ngaythang.chuyenngay(date_ngaysinh.Text);
            else
                ngaysinh = ngaythang.chuyenngay("01/01/1999");
            Boolean gioitinh=false;
            if (radio_nam.Checked == true)
                gioitinh = true;
            else if (radio_nu.Checked == true)
                gioitinh = false;
            string dienthoai = txt_dienthoai.Text;
            string diachi = txt_diachi.Text;
            decimal luongcoban = Convert.ToDecimal("0" + txt_luongcoban.Text.Replace(",", ""));
            decimal hesoluong = Convert.ToDecimal("0" + txt_hesoluong.Text);
            
 
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertnhanvien(tennhanvien, chucvu, maphongban, gioitinh, ngaysinh, dienthoai, diachi, luongcoban, hesoluong);
                                                         

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                else
                {
                    MessageBox_LPV.Show("Thêm mới thành công");
                }

            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                chiso = ((ColumnView)luoi_nhanvien.Views[0]).FocusedRowHandle;
                string manhanvien = grid_nhanvien.GetRowCellValue(grid_nhanvien.FocusedRowHandle, col_manhanvien).ToString();
                ketqua = f_updatenhanvien(manhanvien, tennhanvien, chucvu, maphongban, gioitinh, ngaysinh, dienthoai.Trim(), diachi, luongcoban, hesoluong);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                else
                {
                    MessageBox_LPV.Show("Sửa thành công");
                }

            }
            luoi_nhanvien.DataSource = null;
            luoi_nhanvien.DataSource = f_selectdmnhanvien();
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grid_nhanvien.SelectRow(chiso);
                grid_nhanvien.FocusedRowHandle = chiso;

            }
            com_gioitinh.Visible = false;
        }


        private void bton_clr_Click(object sender, EventArgs e)
        {
            txt_tenphongban.Text = "";
            txt_sotienthoai.Text ="";
        }
       
        private void bton_tim_Click(object sender, EventArgs e)
        {
            string tenphongban = "";
            string sodienthoai = "";
            if (txt_tenphongban.Text != "")
                tenphongban = "'%" + txt_tenphongban.Text + "%'";
            else
                tenphongban = txt_tenphongban.Text;

            if (txt_sotienthoai.Text != "")
                sodienthoai = "'%" + txt_sotienthoai.Text + "%'";
            else
                sodienthoai = txt_sotienthoai.Text;

            luoi_phongban.DataSource = null;
            luoi_phongban.DataSource = f_selecttenphongbandk(tenphongban, sodienthoai);
        }

        private void bton_clrnhanvien_Click(object sender, EventArgs e)
        {
            txt_tennhanvien.Text = "";
            txt_chucvu.Text = "";
            com_phongban.Text = "--- Chọn phòng ban ---";
            //aaa
           // date_ngaysinh.Text = DateTime.Today.ToString("dd/MM/yyyy");
            date_ngaysinh.DateTime = DateTime.Today;
            radio_nam.Checked = true;
            txt_dienthoai.Text = "";
            txt_diachi.Text = "";
            txt_luongcoban.Text = "";
            txt_hesoluong.Text = "";
            bton_tennhanvien.Enabled = true;
            bton_chucvu.Enabled = true;
            bton_dienthoai.Enabled = true;
            bton_diachi.Enabled = true;
            bton_luongcoban.Enabled = true;
            bton_hesoluong.Enabled = true;
            check_ngaysinh.Visible = true;
            com_gioitinh.Visible = true;
            com_gioitinh.Text = "--- Chọn tất cả ---";
            radio_nam.Visible = false;
            radio_nu.Visible = false;
        }

        private void grid_phongban_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_phongban.FocusedColumn;
            txt_tenphongban.Text = (grid_phongban.GetRowCellDisplayText(chiso, col_tenphongban) + "").Trim();
            txt_sotienthoai.Text = (grid_phongban.GetRowCellDisplayText(chiso, col_sodienthoai) + "").Trim();
            bton_luu.Enabled = false;
            bton_tenphongban.Enabled = false;
            bton_sotienthoai.Enabled = false;
        }

        private void grid_nhanvien_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_nhanvien.FocusedColumn;
            
            txt_tennhanvien.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_tennhanvien) + "";
            txt_chucvu.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_chuvu) + "";
            if (grid_nhanvien.GetRowCellDisplayText(chiso, col_phongban_nv) + "" == "")
                com_phongban.Text = "--- Chọn phòng ban ---";
            else
                 com_phongban.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_phongban_nv) + "";
            //aaa
            date_ngaysinh.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_ngaysinh) + "";
            txt_dienthoai.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_dienthoai) + "";
            txt_diachi.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_diachi) + "";
            txt_dienthoai.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_dienthoai) + "";
            txt_luongcoban.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_luongcoban) + "";
            txt_hesoluong.Text = grid_nhanvien.GetRowCellDisplayText(chiso, col_hesoluong) + "";

       
            string gioitinh = grid_nhanvien.GetRowCellDisplayText(chiso, col_gioitinh) + "";
            if (gioitinh == "Checked")
                radio_nam .Checked = true;
            else if (gioitinh == "Unchecked")
                radio_nu.Checked = true;

            bton_luunhanvien.Enabled = false;
            bton_tenphongban.Enabled = false;
            bton_sotienthoai.Enabled = false;
          
            bton_tennhanvien.Enabled = false;
            bton_chucvu.Enabled = false;
            bton_dienthoai.Enabled = false;
            bton_diachi.Enabled = false;
            bton_luongcoban.Enabled = false;
            bton_hesoluong.Enabled = false;
            check_ngaysinh.Visible = false;
            //radio_nam.Visible = true;
            //radio_nu.Visible = true;
            //com_gioitinh.Visible = false;
        }

        private void bton_tenphongban_Click(object sender, EventArgs e)
        {
            
            txt_tenphongban.Text = new cpublic().loadformchu_point(300,200);
        }

        private void bton_sotienthoai_Click(object sender, EventArgs e)
        {
         
            txt_sotienthoai.Text = new cpublic().loadformchu_point(300, 200);
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {

                this.Close();
            }
        }

        private void bton_thoatnhanvien_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

                this.Close();
            //}
        }

        private void bton_timnhanvien_Click(object sender, EventArgs e)
        {
            com_gioitinh.Visible = true;
             luoi_nhanvien.DataSource = null;
           
             luoi_nhanvien.DataSource = f_findnhanvien();
             check_ngaysinh.Visible = true;
                
        }

        private void bton_tennhanvien_Click(object sender, EventArgs e)
        {
            txt_tennhanvien.Text = new cpublic().loadformchu_point(350,250);
        }

        private void bton_chucvu_Click(object sender, EventArgs e)
        {
            txt_chucvu.Text = new cpublic().loadformchu_point(350, 250);
        }

        private void bton_dienthoai_Click(object sender, EventArgs e)
        {
            txt_dienthoai.Text = new cpublic().loadformchu_point(350, 250);
        }

        private void bton_hesoluong_Click(object sender, EventArgs e)
        {
            txt_hesoluong.Text = new cpublic().loadformso_point(700, 250);
        }

        private void bton_luongcoban_Click(object sender, EventArgs e)
        {
            txt_luongcoban.Text = new cpublic().loadformso_point(700, 250);
        }

        private void bton_diachi_Click(object sender, EventArgs e)
        {
            txt_diachi.Text = new cpublic().loadformchu_point(350, 250);
        }

        private void txt_luongcoban_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_hesoluong_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void bton_xemchitietnhanvien_Click(object sender, EventArgs e)
        {
            chitiet = 0;
            
           
            tab_nhanvien.SelectedTabPage = page_nhanvien;
         

        }

        private void tab_nhanvien_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            // hien thi danh sach nhan vien khi chon phong ban
            if (chitiet == 0)
            {
                string maphongban = "";
                luoi_phongban.MainView = grid_phongban;
                if (luoi_phongban.MainView.RowCount > 0)
                    maphongban = grid_phongban.GetRowCellValue(grid_phongban.FocusedRowHandle, col_maphongban).ToString();
                luoi_nhanvien.MainView = grid_nhanvien;
                luoi_nhanvien.DataSource = null;
                luoi_nhanvien.DataSource = f_selectdsnhanvientheophongban(maphongban);
            }
            else
            {
                luoi_nhanvien.MainView = grid_nhanvien;
                grid_nhanvien.OptionsBehavior.Editable = false;
                luoi_nhanvien.DataSource = f_selectdmnhanvien();
                col_gioitinh.FieldName = "GIOITINH";

            }
            if (grid_nhanvien.RowCount > 0)
            {

                txt_tennhanvien.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_tennhanvien).ToString();
                txt_chucvu.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_chuvu).ToString();
                com_phongban.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_phongban_nv).ToString();
                //aaa
                date_ngaysinh.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_ngaysinh).ToString();
                txt_dienthoai.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_dienthoai).ToString();
                txt_diachi.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_diachi).ToString();
                txt_dienthoai.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_dienthoai).ToString();
                txt_luongcoban.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_luongcoban).ToString();
                txt_hesoluong.Text = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_hesoluong).ToString();
                string gioitinh = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_gioitinh).ToString();
                if (gioitinh == "Checked")
                    radio_nam.Checked = true;
                else if (gioitinh == "Unchecked")
                    radio_nu.Checked = true;

            }
            chitiet = 1;
            tv.f_showdataoncombox(com_phongban, f_selectdsphongbanforcom(), "OBJID", "OBJNAME");


        }

        private void txt_dienthoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) != 40 && Convert.ToInt32(e.KeyChar) != 41 && Convert.ToInt32(e.KeyChar) != 45 &&
              Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && (Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57))
                e.Handled = true;
        }

        private void date_ngaysinh_Click(object sender, EventArgs e)
        {
            string date = new cpublic().loadformso_point(670, 270);
            if(date !="")
                   date_ngaysinh.SelectedText = date;

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void bton_chonnhanvien_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            if (grid_nhanvien.RowCount > 0)
            {
                //Variable.manhanvien = grid_nhanvien.GetRowCellDisplayText(grid_nhanvien.FocusedRowHandle, col_manhanvien) + "";
                this.DialogResult = DialogResult.OK;
            }
            this.Close();

        }

        public DataRow ShowForm()
        {
            this.ShowDialog();
            if (this.DialogResult == DialogResult.OK)
                return grid_nhanvien.GetDataRow(grid_nhanvien.FocusedRowHandle);
            return null;
        }
      
    }
}